#!/usr/bin/python

import sys,string
from socket import *
import hashlib 
import random
server_host_port=[
        ["127.0.0.1",12324],
        ["127.0.0.1",12322],
        ["127.0.0.1",12323]
        ]

sock_con_fd=[]

def store_key(key):
    host_index = int(hashlib.md5(key).hexdigest(),16) % len(server_host_port)
    print host_index
    now_host_hostname = server_host_port[host_index][0]
    now_host_port = server_host_port[host_index][1]
    print now_host_hostname 
    print now_host_port
    # connect to server on the port
    request ='set ' + key + ' 0 0 3 123'
    print request
    sock_con_fd[host_index][0].send(request)

def connect_host():
    for host_index in range(len(server_host_port)):
        sock_con_fd.append([])
        sock_fd = socket(AF_INET, SOCK_STREAM)
        print sock_fd
        conn_fd = sock_fd.connect((server_host_port[host_index][0],server_host_port[host_index][1]))
        print conn_fd
        sock_con_fd[host_index].insert(0,sock_fd)
        sock_con_fd[host_index].insert(1,conn_fd)
    for item in sock_con_fd:
        print item

if __name__=="__main__":
    print("now in main")
    connect_host()
    for j in range(1,1000):
        key = string.join(random.sample(['z','y','x','w','v','u','t','s','r','a'], 5)).replace(' ','') 
        print(key)
        store_key(key)
